package com.webcash.CNBoard.Controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.webcash.CNBoard.Model.CreateWork;
import com.webcash.CNBoard.Model.Document;
import com.webcash.CNBoard.Model.Employee;
import com.webcash.CNBoard.Model.Login;
import com.webcash.CNBoard.Model.MyProject;
import com.webcash.CNBoard.Model.Project;
import com.webcash.CNBoard.Model.ProjectFile;
import com.webcash.CNBoard.Model.Work;
import com.webcash.CNBoard.Model.WorkDocFileTracer;
import com.webcash.CNBoard.Model.WorkInfoInProject;
import com.webcash.CNBoard.Model.WorkTracer;
import com.webcash.CNBoard.Service.EmployeeService;
import com.webcash.CNBoard.Service.ProjectFileService;
import com.webcash.CNBoard.Service.ProjectService;

@Controller
@RequestMapping("/project")
@SessionAttributes("login")
public class ProjectMyPageController {

	@Autowired
	EmployeeService employeeService;
	@Autowired
	ProjectService projectService;
	@Autowired
	ProjectFileService projectFileService;
	@Autowired
	MyProject myProject;

	@RequestMapping
	public String action(@ModelAttribute Login login, Model model) {

		List<Project> projectList = projectService.getProjectListById(login
				.getId());
		model.addAttribute("projectList", projectList);

		myProject.setId(login.getId());

		return "project/projectPage";
	}

	@RequestMapping("/projectMain")
	// 비동기적으로 projectNo 저장하기
	@ResponseBody
	public String actionMain(@RequestParam int projectNo, Model model) {
		myProject.setProjectNo(projectNo);
		
		return "";
	}

	@RequestMapping("/projectMainAction")
	// 프로젝트 선택 후 페이지 이동
	public String mainAction(Model model) {
		Project project = projectService.getProjectByProjectNo(myProject
				.getProjectNo());

		WorkInfoInProject workInfo = projectService
				.getWorkListInfoByProjectNo(myProject.getProjectNo());
		
		model.addAttribute("project", project);
		model.addAttribute("workInfo", workInfo);

		return "project/projectMain";
	}

	@RequestMapping("/createWork")
	// 새로운 업무 만들기 페이지
	public String createWork(@ModelAttribute Login login, Model model) {
		return "project/createWork";
	}
	@RequestMapping("/createWork/childWork")
	// 새로운 업무 만들기 페이지
	public String createChildWork(@RequestParam int workNo,  Model model) {
		System.out.println("come here" + workNo);
		model.addAttribute("upperWorkNo", workNo);
		return "project/createWork";
	}

	@RequestMapping(value = "/createWork/action")
	// 새로운 업무 등록 action
	@ResponseBody
	public String workList2(@RequestParam int docNo, @RequestParam int fileNo, @ModelAttribute CreateWork createWork, Model model) {
		
		String result = projectService.addCreateWorkByWorkAndProjectNo(createWork,
				myProject.getProjectNo(), docNo, fileNo);
		
		return result;

	}
	@RequestMapping(value = "/modifyWork/action")
	// 새로운 업무 등록 action
	@ResponseBody
	public String modifyList2(@RequestParam int docNo, @RequestParam int fileNo, @RequestParam int relationNo, @RequestParam int relationNo2, @RequestParam int workNo, @ModelAttribute CreateWork createWork, Model model) {
		String result = projectService.modifyCreateWorkByWorkAndProjectNo(createWork, myProject.getProjectNo(), workNo, relationNo, relationNo2, docNo, fileNo);
		
		return result;
		
	}

	@RequestMapping(value = "/work/{workNo}")
	public String workPage(@PathVariable int workNo, Model model) {
		Work work = projectService.getWorkByWorkNo(workNo);
		Work upperWork = projectService.getUpperWorkByDownerWorkNo(workNo);
		List<Work> downerWorkList = projectService.getDownerWorkListByProjectNoAndWorkNo(myProject.getProjectNo(), workNo);
		
		WorkDocFileTracer workDocFileTracer = projectService.getWorkDocFileTracerByWorkNo(workNo);
		
		ProjectFile file = null;
		Document doc = null;
		
		if(workDocFileTracer != null){
			file = projectFileService.getProjectFileByFileNo(workDocFileTracer.getFileNo());
			doc = projectService.getDocumentByDocNo(workDocFileTracer.getDocNo());
		}
		model.addAttribute("work", work);
		if(upperWork != null)
			model.addAttribute("upperWork", upperWork);
		if(downerWorkList != null)
			model.addAttribute("downerWorkList", downerWorkList);
		if(file != null)
			model.addAttribute("file", file);
		if(doc != null)
			model.addAttribute("doc", doc);
		
		return "project/work";
	}
	
	
	@RequestMapping(value="/work/{workNo}/modify")
	public String workModifyPage(@PathVariable int workNo, Model model){
		
		Work work = projectService.getWorkByWorkNo(workNo);
		Date startDate = work.getWorkStartTime();
		Date deadDate = work.getWorkDeadTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		WorkTracer workTracer = projectService.getWorkTracerByDownerWorkNo(workNo);
		
		WorkDocFileTracer workDocFileTracer = projectService.getWorkDocFileTracerByWorkNo(workNo);
		
		if(startDate != null)
			model.addAttribute("startDateFormat", format.format(startDate));
		if(deadDate != null)
			model.addAttribute("deadDateFormat", format.format(deadDate));
		if(workDocFileTracer != null)
			model.addAttribute("workDocFileTracer", workDocFileTracer);
		if(workTracer != null)
			model.addAttribute("workTracer", workTracer);
		
		model.addAttribute("work", work);
		return "project/workModify";
	}
	
	@RequestMapping(value="/work/updateUpperWork")
	public String updateUpperWork(@RequestParam int workNo, @RequestParam int upperWorkNo, Model model){
		
		projectService.addWorkWithUpperWork(workNo,myProject.getProjectNo(), upperWorkNo);
		
		
		Work work = projectService.getWorkByWorkNo(workNo);
		Work upperWork = projectService.getUpperWorkByDownerWorkNo(workNo);
		List<Work> downerWorkList = projectService.getDownerWorkListByProjectNoAndWorkNo(myProject.getProjectNo(), workNo);
		
		model.addAttribute("work", work);
		if(upperWork != null){
			model.addAttribute("upperWork", upperWork);
		}
		if(downerWorkList != null)
			model.addAttribute("downerWorkList", downerWorkList);
		
		return "project/work";
	}
	@RequestMapping(value="/work/updateDocument")
	public String updateDocument(@RequestParam int workNo, @RequestParam int docNo, Model model){
		
		projectService.addWorkWithDocument(workNo, myProject.getProjectNo(), docNo);
		
		Work work = projectService.getWorkByWorkNo(workNo);
		Work upperWork = projectService.getUpperWorkByDownerWorkNo(workNo);
		List<Work> downerWorkList = projectService.getDownerWorkListByProjectNoAndWorkNo(myProject.getProjectNo(), workNo);
		
		WorkDocFileTracer workDocFileTracer = projectService.getWorkDocFileTracerByWorkNo(workNo);
		
		ProjectFile file = null;
		Document doc = null;
		
		if(workDocFileTracer != null){
			file = projectFileService.getProjectFileByFileNo(workDocFileTracer.getFileNo());
			doc = projectService.getDocumentByDocNo(workDocFileTracer.getDocNo());
		}
		
		model.addAttribute("work", work);
		if(upperWork != null){
			model.addAttribute("upperWork", upperWork);
		}
		if(downerWorkList != null)
			model.addAttribute("downerWorkList", downerWorkList);
		if(file != null)
			model.addAttribute("file", file);
		if(doc != null)
			model.addAttribute("doc", doc);
		return "project/work";
	}
	@RequestMapping(value="/work/updateFile")
	public String updateFile(@RequestParam int workNo, @RequestParam int fileNo, Model model){
		
		projectService.addWorkWithFile(workNo, myProject.getProjectNo(), fileNo);
		
		Work work = projectService.getWorkByWorkNo(workNo);
		Work upperWork = projectService.getUpperWorkByDownerWorkNo(workNo);
		List<Work> downerWorkList = projectService.getDownerWorkListByProjectNoAndWorkNo(myProject.getProjectNo(), workNo);
		
		WorkDocFileTracer workDocFileTracer = projectService.getWorkDocFileTracerByWorkNo(workNo);
		
		ProjectFile file = null;
		Document doc = null;
		
		if(workDocFileTracer != null){
			file = projectFileService.getProjectFileByFileNo(workDocFileTracer.getFileNo());
			doc = projectService.getDocumentByDocNo(workDocFileTracer.getDocNo());
		}
		
		model.addAttribute("work", work);
		if(upperWork != null){
			model.addAttribute("upperWork", upperWork);
		}
		if(downerWorkList != null)
			model.addAttribute("downerWorkList", downerWorkList);
		if(file != null)
			model.addAttribute("file", file);
		if(doc != null)
			model.addAttribute("doc", doc);
		
		return "project/work";
	}
	
	@RequestMapping(value="/work/delete")
	@ResponseBody
	public String deleteWork(@RequestParam int workNo){
		projectService.removeWorkByWorkNo(workNo);
		
		return "";
	}
	

	@RequestMapping(value = "/pickWork/{workNo}")
	public String pickWork(@PathVariable int workNo, Model model) {
		Work work = projectService.getWorkByWorkNo(workNo);
		myProject.setProjectNo(work.getProjectNo());
		
		Work upperWork = projectService.getUpperWorkByDownerWorkNo(workNo);
		List<Work> downerWorkList = projectService.getDownerWorkListByProjectNoAndWorkNo(myProject.getProjectNo(), workNo);
		
		WorkDocFileTracer workDocFileTracer = projectService.getWorkDocFileTracerByWorkNo(workNo);
		
		ProjectFile file = null;
		Document doc = null;
		
		if(workDocFileTracer != null){
			file = projectFileService.getProjectFileByFileNo(workDocFileTracer.getFileNo());
			doc = projectService.getDocumentByDocNo(workDocFileTracer.getDocNo());
		}
		
		model.addAttribute("work", work);
		if(upperWork != null)
			model.addAttribute("upperWork", upperWork);
		if(downerWorkList != null)
			model.addAttribute("downerWorkList", downerWorkList);
		if(file != null)
			model.addAttribute("file", file);
		if(doc != null)
			model.addAttribute("doc", doc);

		return "project/projectWork";
	}

	@RequestMapping(value = "/createWork/getIdData")
	@ResponseBody
	public ResponseEntity<String> getIdData(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		List<Employee> empList = projectService.getMemberByProjectNo(myProject
				.getProjectNo());

		String jsonArray = new String("[");
		
		int count = 0;
		for (Employee emp : empList) {
			jsonArray = jsonArray + "{name:\"" + emp.getName() + "\",to:" + emp.getEmpNo() + ",id:\"" + emp.getId() + "\"}";
			count++;
			if(count != empList.size()){
				jsonArray = jsonArray + ",";
			}
		}
		jsonArray = jsonArray + "]";
		
		
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=UTF-8");
		
		return new ResponseEntity<String>(jsonArray, responseHeaders,
				HttpStatus.CREATED);

	}
	@RequestMapping(value = "/createWork/getWorkData")
	@ResponseBody
	public ResponseEntity<String> getWorkData(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		
		List<Work> empList = projectService.getWorkListByProjectNo(myProject.getProjectNo());
		
		String jsonArray = new String("[");
		
		int count = 0;
		for (Work work : empList) {
			jsonArray = jsonArray + "{title:\"" + work.getWorkTitle() + "\",to:" + work.getWorkNo()+ ",state:\"" + work.getWorkState() + "\"}";
			count++;
			if(count != empList.size()){
				jsonArray = jsonArray + ",";
			}
		}
		jsonArray = jsonArray + "]";
		
		
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=UTF-8");
		
		return new ResponseEntity<String>(jsonArray, responseHeaders,
				HttpStatus.CREATED);
		
	}
	@RequestMapping(value = "/createWork/getDocData")
	@ResponseBody
	public ResponseEntity<String> getDocData(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		
		List<Document> docList = projectService.getDocumentListByProjectNo(myProject.getProjectNo());
		
		String jsonArray = new String("[");
		
		int count = 0;
		for (Document doc : docList) {
			jsonArray = jsonArray + "{title:\"" + doc.getDocTitle() + "\",to:" + doc.getDocNo()+ ",writer:\"" + doc.getDocWriter() + "\"}";
			count++;
			if(count != docList.size()){
				jsonArray = jsonArray + ",";
			}
		}
		jsonArray = jsonArray + "]";
		
		
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=UTF-8");
		
		return new ResponseEntity<String>(jsonArray, responseHeaders,
				HttpStatus.CREATED);
		
	}
	@RequestMapping(value = "/createWork/getFileData")
	@ResponseBody
	public ResponseEntity<String> getFileData(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		
		List<ProjectFile> fileList = projectFileService.getProjectFileListByProjectNo(myProject.getProjectNo());
		
		String jsonArray = new String("[");
		
		int count = 0;
		for (ProjectFile file : fileList) {
			jsonArray = jsonArray + "{title:\"" + file.getFileName() + "\",to:" + file.getFileNo()+ ",size:\"" + file.getFileSize() + "\"}";
			count++;
			if(count != fileList.size()){
				jsonArray = jsonArray + ",";
			}
		}
		jsonArray = jsonArray + "]";
		
		
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=UTF-8");
		
		return new ResponseEntity<String>(jsonArray, responseHeaders,
				HttpStatus.CREATED);
		
	}
}
